package com.flx.dis.read.write.split.controller;

import com.flx.common.utils.result.ResultResponse;
import com.flx.dis.read.write.split.entity.Book;
import com.flx.dis.read.write.split.service.BookService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Fenglixiong
 * @Date: 2021/6/9 17:54
 * @Description: work用来做读写分离测试，增删改走主库，查询走从库
 */
@Slf4j
@Api
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping("/add")
    public ResultResponse add(@RequestBody Book book){
        try {
            Long id = bookService.add(book);
            return ResultResponse.success(id.toString());
        }catch (Exception e){
            return ResultResponse.error(e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public ResultResponse get(@PathVariable("id")Long id){
        try {
            Book book = bookService.get(id);
            return ResultResponse.success(book);
        }catch (Exception e){
            return ResultResponse.error(e.getMessage());
        }
    }

}
